<template>
  <view class="page">
    <!-- 搜索 -->
    <mesSearch
      :placeholder="placeholder"
      v-model="postForm.purchase_code"
      @search="resetPaging(), getData()"
    ></mesSearch>
    <view class="division">
      <mes-card-index :form="config"></mes-card-index>
    </view>
    <mes-tip v-if="noData"></mes-tip>
    <!-- 按钮 -->
    <mes-button type="1" :form="buttonFrom"></mes-button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      hasMore: true,
      noData: false,
      buttonFrom: {
        addEvent: () => {
          uni.navigateTo({
            url: '/pages/home/outsource/add'
          })
        }
      },
      placeholder: '单据编号',
      postForm: {
        purchase_code: '',
        page: 1
      },
      config: {
        items: [],
        actionSheet: [
          {
            name: '审核/反审核',
            event: (row) => {}
          },
          {
            name: '详情',
            event: (row) => {
              let obj = {
                id: row.id,
                status: row.examine_status
              }
              obj = JSON.stringify(obj)
              uni.navigateTo({
                url: `/pages/home/outsource/details?obj=${obj}`
              })
            }
          },
          {
            name: '编辑',
            event: (row) => {
              if (row.examine_status == '1') {
                return this.$tools.toast('不能编辑已审核单据')
              }
              let obj = {
                id: row.id,
                status: row.examine_status
              }
              obj = JSON.stringify(obj)
              uni.navigateTo({
                url: `/pages/home/outsource/edit?obj=${obj}`
              })
            }
          },
          {
            name: '删除',
            showModal: true,
            event: (row) => {
              this.del(row)
            }
          }
        ],
        more: (row) => {
          console.log('调用more')
          let examine = row.examine_status == '1' ? 'examine_f' : 'examine'
          this.config.actionSheet[0] = {
            name: examine == 'examine' ? '审核' : '反审核',
            event: () => {
              let postForm = {
                id: row.id,
                type: examine
              }
              this.$api.prod.outsourcing
                .examine(postForm)
                .then((res) => {
                  if (res.code == 200) {
                    this.$tools.toast(res.msg)
                    this.resetPaging()
                    this.getData()
                  }
                })
                .catch()
            }
          }
        }
      }
    }
  },
  onShow() {
    this.resetPaging()
    this.getData()
  },
  onReachBottom() {
    if (!this.hasMore) return
    this.postForm.page++
    this.getData()
  },
  methods: {
    // 重置分页
    resetPaging() {
      this.hasMore = true
      this.postForm.page = 1
      this.config.items = []
    },
    //删除
    async del(row) {
      const res = await this.$api.prod.outsourcing.del({ id: row.id })
      if (res.code == 200) {
        this.$tools.toast(res.msg)
        this.resetPaging()
        this.getData()
      }
    },
    // 获取列表
    async getData() {
      const res = await this.$api.prod.outsourcing.index(this.postForm)
      if (res.code == 200) {
        this.hasMore = res.data.list.length ? true : false
        const list = []
        res.data.list.forEach((item) => {
          list.push({
            row: item,
            head: {
              tags: [
                {
                  name: item.examine_status == '1' ? '已审核' : '未审核',
                  status: item.examine_status
                }
              ],
              title: item.purchase_code,
              price: item.all_money,//金额
            },
            body: [
              {
                name: '单据日期',
                value: item.purchase_time
              },
              {
                name: '开工时间',
                value: item.work_time
              },
              {
                name: '委外工厂',
                value: item.goods_info
              },
              {
                name: '委外费用',
                value: item.free_money
              },
              {
                name: '原材料数量',
                value: item.number
              }
            ]
          })
        })
        this.config.items = [...this.config.items, ...list]
        res.data.count == 0 ? (this.noData = true) : (this.noData = false)
      }
    }
  }
}
</script>

<style>
.division {
  margin-top: 100rpx;
}
</style>
